/**
 * StarInfoFrameExploreColonize.java
 *
 * Roslan's Remake of MOO in 3D using Ardor3D
 * (C) 2011 Roslan Amir. All rights reserved.
 */

package com.roslan.games.moo3d.ui.hud;

import com.ardor3d.extension.ui.UIButton;
import com.ardor3d.extension.ui.UILabel;
import com.ardor3d.extension.ui.UIPanel;
import com.ardor3d.extension.ui.backdrop.SolidBackdrop;
import com.ardor3d.extension.ui.backdrop.UIBackdrop;
import com.ardor3d.extension.ui.event.ActionEvent;
import com.ardor3d.extension.ui.event.ActionListener;
import com.ardor3d.extension.ui.layout.AnchorLayout;
import com.ardor3d.extension.ui.layout.AnchorLayoutData;
import com.ardor3d.extension.ui.layout.RowLayout;
import com.ardor3d.extension.ui.util.Alignment;
import com.ardor3d.extension.ui.util.Insets;
import com.ardor3d.extension.ui.util.SubTex;
import com.ardor3d.image.Texture;
import com.ardor3d.math.ColorRGBA;
import com.ardor3d.util.TextureManager;
import com.roslan.games.moo3d.GameData.Actions;
import com.roslan.games.moo3d.data.Star;
import com.roslan.games.moo3d.spatial.StarNode;

/**
 * This Panel displays the information about the just explored Star.
 * 
 * @author Roslan Amir
 * @version 1.0 - Jan 31, 2011
 */
public class StarInfoFrameExploreColonize extends StarInfoFrameExploredBase {

	private static final String COLOR_FERTILE = "[c=#0a0]"; // green

	private static final String COLOR_HOSTILE = "[c=#a00]"; // red

	private static final String COLOR_RICH = "[c=#00a]"; // light blue

	private static final String COLOR_POOR = "[c=#a64]"; // brown

	private static final String COLOR_ARTIFACTS = COLOR_FERTILE; // green

	private static final String COLOR_INFO = "[c=#aaa]"; // light grey

	/**
	 * 
	 */
	public static final String IMAGE_EXPLORE = "images/explore_new_star.png";

	/**
	 * 
	 */
	public static final String IMAGE_COLONIZE = "images/build_new_colony.png";

	/**
	 * The 6-line labels for the main information text.
	 */
	private final UILabel[] infoValueLabel = new UILabel[6];

	/**
	 * The label for the star's range.
	 */
	private final UILabel rangeValueLabel;

	/**
	 * The button to close this Panel.
	 */
	private final UIButton closeNoButton;

	/**
	 * The button to colonize the star.
	 */
	private final UIButton colonizeButton;

	/**
	 * Work variable: StringBuilder
	 */
	private final transient StringBuilder sb = new StringBuilder();

	/**
	 * Constructor.
	 * 
	 * @param colonize - <code>true</code> if this is for the "Colonize" panel, <code>false</code> if it is for the "Explore"
	 *            panel
	 */
	public StarInfoFrameExploreColonize(boolean colonize) {
		super("StarInfoFrameExplored");

		final UIBackdrop solidBackDrop = new SolidBackdrop(ColorRGBA.BLACK);
		final UIPanel panel = new UIPanel();
		panel.setBackdrop(solidBackDrop);
		panel.setLayout(new AnchorLayout());
		panel.setMargin(HudManager.MARGINS_FOUR);
		panel.setBorder(HudManager.SOLID_BORDER_TWO);
		infoPanel.add(panel);

		int ypos = -20;
		final Texture tex = TextureManager.load((colonize) ? IMAGE_COLONIZE : IMAGE_EXPLORE, Texture.MinificationFilter.Trilinear, false);
		final SubTex headerLabelImage = new SubTex(tex);
		final UILabel headerLabel = new UILabel("", headerLabelImage);
		headerLabel.setGap(0);
		headerLabel.setLayoutData(new AnchorLayoutData(Alignment.TOP_LEFT, panel, Alignment.TOP_LEFT, 4, ypos));
		panel.add(headerLabel);

		ypos -= headerLabelImage.getHeight();
		for (int i = 0; i < infoValueLabel.length; i++) {
			infoValueLabel[i] = new UILabel(" ");
			infoValueLabel[i].setStyledText(true);
			infoValueLabel[i].setLayoutData(new AnchorLayoutData(Alignment.TOP_LEFT, panel, Alignment.TOP_LEFT, 4, ypos));
			panel.add(infoValueLabel[i]);
			ypos -= 18;
		}

//		ypos -= 18;
		rangeValueLabel = new UILabel("");
		rangeValueLabel.setStyledText(true);
		rangeValueLabel.setLayoutData(new AnchorLayoutData(Alignment.TOP_LEFT, panel, Alignment.TOP_LEFT, 4, ypos));
		rangeValueLabel.setMargin(new Insets(0, 0, 12, 0));
		panel.add(rangeValueLabel);

		/*
		 * Close, Colonize buttons
		 */
		final UIPanel hpanel2 = new UIPanel();
		hpanel2.setLayout(new RowLayout(true));
		hpanel2.setPadding(HudManager.PADDING_TWO);
		hpanel2.setMargin(HudManager.MARGINS_FOUR);
		hpanel2.setBorder(HudManager.SOLID_BORDER_TWO);
		infoPanel.add(hpanel2);

		if (colonize) {
//			colonizeButton = new UIButton("[b]Yes[/b]");
			colonizeButton = new UIButton("Yes");
			colonizeButton.setStyledText(true);
			colonizeButton.addActionListener(new ActionListener() {
				public void actionPerformed(final ActionEvent event) {
					gameData.action = Actions.SHOW_COLONIZED_INFO;
				}
			});
			hpanel2.add(colonizeButton);
//			closeNoButton = new UIButton("[b]No[/b]");
			closeNoButton = new UIButton("No");
		} else {
			colonizeButton = null;
//			closeNoButton = new UIButton("[b]Close[/b]");
			closeNoButton = new UIButton("Close");
		}

		closeNoButton.setStyledText(true);
		closeNoButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				gameData.action = Actions.SHOW_EVENT;
			}
		});
		hpanel2.add(closeNoButton);
	}

	/**
	 * Update the information to the currently selected Star (or Colony).
	 * 
	 * @param starNode - the Star (or Colony) whose info is to be updated
	 */
	@Override
	public void update(StarNode starNode) {
		super.update(starNode);

		Star star = starNode.getStar();
		int index = 0;
		switch (star.fertility) {
		case FERTILE:
			sb.setLength(0);
//			sb.append("[b]");
			sb.append(COLOR_FERTILE);
			sb.append("Ecologically Fertile");
			sb.append("[/c]");
//			sb.append("[/b]");
			infoValueLabel[index++].setText(sb.toString());
			sb.setLength(0);
//			sb.append("[b]");
			sb.append(COLOR_INFO);
			sb.append("(Population growth is +50% normal) ");
			sb.append("[/c]");
//			sb.append("[/b]");
			infoValueLabel[index++].setText(sb.toString());
			break;
		case HOSTILE:
			sb.setLength(0);
//			sb.append("[b]");
			sb.append(COLOR_HOSTILE);
			sb.append("Hostile Environment");
			sb.append("[/c]");
//			sb.append("[/b]");
			infoValueLabel[index++].setText(sb.toString());
			sb.setLength(0);
//			sb.append("[b]");
			sb.append(COLOR_INFO);
			sb.append("(Population growth is halved) ");
			sb.append("[/c]");
//			sb.append("[/b]");
			infoValueLabel[index++].setText(sb.toString());
			break;
		default:
			break;
		}
		switch (star.minerals) {
		case POOR:
			sb.setLength(0);
//			sb.append("[b]");
			sb.append(COLOR_POOR);
			sb.append("Mineral Poor");
			sb.append("[/c]");
//			sb.append("[/b]");
			infoValueLabel[index++].setText(sb.toString());
			sb.setLength(0);
//			sb.append("[b]");
			sb.append(COLOR_INFO);
			sb.append("(Resource points from industry are halved) ");
			sb.append("[/c]");
//			sb.append("[/b]");
			infoValueLabel[index++].setText(sb.toString());
			break;
		case RICH:
			sb.setLength(0);
//			sb.append("[b]");
			sb.append(COLOR_RICH);
			sb.append("Mineral Rich");
			sb.append("[/c]");
//			sb.append("[/b]");
			infoValueLabel[index++].setText(sb.toString());
			sb.setLength(0);
//			sb.append("[b]");
			sb.append(COLOR_INFO);
			sb.append("(Resource points from industry are doubled) ");
			sb.append("[/c]");
//			sb.append("[/b]");
			infoValueLabel[index++].setText(sb.toString());
			break;
		case ULTRA_POOR:
			sb.setLength(0);
//			sb.append("[b]");
			sb.append(COLOR_POOR);
			sb.append("Mineral Ultra Poor");
			sb.append("[/c]");
//			sb.append("[/b]");
			infoValueLabel[index++].setText(sb.toString());
			sb.setLength(0);
//			sb.append("[b]");
			sb.append(COLOR_INFO);
			sb.append("(Resource points from industry are one-third) ");
			sb.append("[/c]");
//			sb.append("[/b]");
			infoValueLabel[index++].setText(sb.toString());
			break;
		case ULTRA_RICH:
			sb.setLength(0);
//			sb.append("[b]");
			sb.append(COLOR_RICH);
			sb.append("Mineral Ultra Rich");
			sb.append("[/c]");
//			sb.append("[/b]");
			infoValueLabel[index++].setText(sb.toString());
			sb.setLength(0);
//			sb.append("[b]");
			sb.append(COLOR_INFO);
			sb.append("(Resource points from industry are tripled) ");
			sb.append("[/c]");
//			sb.append("[/b]");
			infoValueLabel[index++].setText(sb.toString());
			break;
		default:
			break;
		}
		switch (star.research) {
		case ARTIFACTS:
			sb.setLength(0);
//			sb.append("[b]");
			sb.append(COLOR_ARTIFACTS);
			sb.append("Artifacts World");
			sb.append("[/c]");
//			sb.append("[/b]");
			infoValueLabel[index++].setText(sb.toString());
			sb.setLength(0);
//			sb.append("[b]");
			sb.append(COLOR_INFO);
			sb.append("(Technology points from research are doubled) ");
			sb.append("[/c]");
//			sb.append("[/b]");
			infoValueLabel[index++].setText(sb.toString());
			break;
		case ORION:
			sb.setLength(0);
//			sb.append("[b]");
			sb.append(COLOR_ARTIFACTS);
			sb.append("Orion");
			sb.append("[/c]");
//			sb.append("[/b]");
			infoValueLabel[index++].setText(sb.toString());
			sb.setLength(0);
//			sb.append("[b]");
			sb.append(COLOR_INFO);
			sb.append("(Technology points from research are quadrupled) ");
			sb.append("[/c]");
//			sb.append("[/b]");
			infoValueLabel[index++].setText(sb.toString());
			break;
		default:
			break;
		}
		while (index < infoValueLabel.length) {
			infoValueLabel[index].setText("");
			index++;
		}

		sb.setLength(0);
//		sb.append("[b]");
		sb.append(COLOR_INFO);
		sb.append("Range ");
		sb.append(starNode.range);
		sb.append("[/c]");
//		sb.append("[/b]");
		rangeValueLabel.setText(sb.toString());

		updateAndpack();
	}
}
